// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package webpagereplay

import (
	"reflect"
	"testing"
)

var derBytes1 []byte = []byte{
	0x30, 0x82, 0x02, 0x0c, 0x30, 0x82, 0x01, 0x75, 0x02, 0x14, 0x74, 0x89, 0xa5, 0x65, 0xd6, 0x58,
	0xfe, 0x24, 0xca, 0x22, 0xa9, 0xf5, 0x19, 0x26, 0xa1, 0x9d, 0xcc, 0x0b, 0xe2, 0x15, 0x30, 0x0d,
	0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x45, 0x31,
	0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11,
	0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74,
	0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65,
	0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79,
	0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x33, 0x30, 0x34, 0x31, 0x38, 0x31, 0x39,
	0x32, 0x34, 0x33, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x31, 0x31, 0x32, 0x31, 0x39, 0x32,
	0x34, 0x33, 0x34, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
	0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
	0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04,
	0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
	0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x81, 0x9f, 0x30, 0x0d,
	0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d,
	0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb2, 0xce, 0xa1, 0x8d, 0x44, 0x34, 0xde, 0xfc,
	0x74, 0xcb, 0xce, 0xdf, 0x7b, 0x52, 0x12, 0x57, 0x5d, 0x69, 0xa3, 0xc5, 0x57, 0x8a, 0x28, 0x5c,
	0x92, 0x9b, 0x0a, 0xa5, 0xac, 0x12, 0xea, 0xe4, 0x28, 0x98, 0xed, 0xa0, 0x7c, 0x67, 0xae, 0x2e,
	0x78, 0xae, 0x03, 0x4e, 0x2d, 0x25, 0x38, 0x3b, 0xc6, 0x90, 0xca, 0xd5, 0x84, 0xf7, 0x3a, 0xba,
	0x12, 0x2f, 0xdc, 0xa8, 0xae, 0xdd, 0x97, 0xa5, 0x2a, 0x66, 0x26, 0x6d, 0xc2, 0x4e, 0x07, 0x56,
	0xb0, 0x4b, 0xef, 0xce, 0x9c, 0x54, 0xa9, 0x60, 0xa4, 0xf9, 0x21, 0x72, 0x71, 0xbe, 0x10, 0xc7,
	0x43, 0xc7, 0x2f, 0xcf, 0xc8, 0xc0, 0xf7, 0xf2, 0x33, 0x6d, 0x4c, 0xd3, 0x09, 0x2e, 0x2c, 0x1b,
	0xc6, 0x26, 0x6a, 0x32, 0x8d, 0xef, 0x8d, 0x9f, 0xf8, 0xfb, 0x55, 0xcb, 0x74, 0xd8, 0xac, 0x01,
	0x64, 0xc3, 0x2d, 0xa5, 0x9e, 0x5f, 0x19, 0x9b, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06,
	0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00,
	0x1f, 0x28, 0xd3, 0x98, 0x61, 0x20, 0x17, 0x32, 0xc8, 0x05, 0xc0, 0x06, 0x9a, 0x40, 0xb2, 0xaa,
	0x69, 0x26, 0x1d, 0x48, 0xbe, 0xbc, 0xc2, 0xf2, 0x33, 0x0a, 0x67, 0xf0, 0x45, 0x03, 0x32, 0x22,
	0x4e, 0xf9, 0x8a, 0xb4, 0x0c, 0x3e, 0x31, 0x9b, 0x75, 0x69, 0x7f, 0x1a, 0x36, 0x8c, 0x42, 0x00,
	0x62, 0xb9, 0xe1, 0xa2, 0x2c, 0x64, 0x19, 0xa9, 0xc9, 0x7f, 0xee, 0x24, 0x67, 0xac, 0x2e, 0x5a,
	0xe1, 0xff, 0x3b, 0x27, 0x53, 0x9c, 0x87, 0xe3, 0xdd, 0x2e, 0x36, 0x7c, 0x30, 0x07, 0xb5, 0x9b,
	0x61, 0xf5, 0x20, 0x93, 0x5f, 0x57, 0xcb, 0x04, 0xa8, 0x5e, 0xfe, 0xf3, 0x60, 0x91, 0x1e, 0x48,
	0xbc, 0xc4, 0x2a, 0xe4, 0xd6, 0x59, 0xc3, 0x9a, 0xee, 0x95, 0xa3, 0x31, 0xf5, 0xa6, 0x35, 0x46,
	0x44, 0x33, 0x03, 0x2a, 0x75, 0x9e, 0xbe, 0x13, 0x6c, 0x53, 0x9f, 0xa6, 0x68, 0x15, 0x24, 0x79,
}

var derBytes2 []byte = []byte{
	0x30, 0x82, 0x01, 0x6d, 0x30, 0x82, 0x01, 0x14, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x11, 0x00,
	0xc4, 0x14, 0xab, 0xd2, 0x52, 0x38, 0xc4, 0x7f, 0xa3, 0xd6, 0xa4, 0x6e, 0x3e, 0x40, 0x78, 0xc0,
	0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x12, 0x31, 0x10,
	0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x07, 0x41, 0x63, 0x6d, 0x65, 0x20, 0x43, 0x6f,
	0x30, 0x1e, 0x17, 0x0d, 0x32, 0x33, 0x31, 0x30, 0x30, 0x34, 0x32, 0x31, 0x33, 0x35, 0x33, 0x36,
	0x5a, 0x17, 0x0d, 0x32, 0x34, 0x31, 0x30, 0x30, 0x33, 0x32, 0x31, 0x33, 0x35, 0x33, 0x36, 0x5a,
	0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x07, 0x41, 0x63, 0x6d,
	0x65, 0x20, 0x43, 0x6f, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
	0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x52,
	0xc3, 0xa1, 0x8e, 0x40, 0xb4, 0x3b, 0xd2, 0xa2, 0xbb, 0xe8, 0xeb, 0xaa, 0x5f, 0x77, 0x08, 0x61,
	0x96, 0x7f, 0x9b, 0x82, 0xa9, 0xef, 0x02, 0xac, 0x54, 0x3d, 0xd1, 0xb4, 0xf7, 0xd3, 0x2f, 0xaf,
	0xd9, 0x7b, 0x0d, 0x6d, 0xa5, 0x75, 0x25, 0x25, 0x5a, 0x12, 0xb6, 0x8a, 0x53, 0xbb, 0xc3, 0x78,
	0x2b, 0x96, 0x21, 0xb3, 0x3d, 0xe5, 0xfc, 0x84, 0x80, 0xe5, 0x82, 0x35, 0x0a, 0xde, 0x19, 0xa3,
	0x4b, 0x30, 0x49, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03,
	0x02, 0x07, 0x80, 0x30, 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08,
	0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
	0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x14, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x0d, 0x30,
	0x0b, 0x82, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x0a, 0x06, 0x08,
	0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x40,
	0x08, 0x5f, 0x71, 0xa9, 0x0e, 0xa4, 0xff, 0x94, 0xc4, 0xa2, 0x76, 0x3d, 0x92, 0xa2, 0xd9, 0xc6,
	0x5c, 0xc0, 0x51, 0x75, 0x5d, 0x07, 0x5b, 0xab, 0xfa, 0xde, 0x19, 0x44, 0x32, 0xb1, 0xc7, 0x02,
	0x20, 0x4e, 0x06, 0x3c, 0xba, 0x1f, 0x58, 0x89, 0xa6, 0x36, 0x6a, 0x7f, 0x4d, 0xc8, 0xd7, 0xc4,
	0x93, 0xad, 0x99, 0x62, 0x8c, 0x4f, 0x1c, 0x9a, 0x0e, 0xef, 0xbe, 0x0d, 0x1f, 0x49, 0x8f, 0xe1,
	0x6b,
}

func TestParseDerBytesWithMultipleCerts(t *testing.T) {
	totalDerBytes := []byte{}
	totalDerBytes = append(totalDerBytes, derBytes1...)
	totalDerBytes = append(totalDerBytes, derBytes2...)
	certBytes := parseDerBytes(totalDerBytes)

	if len(certBytes) != 2 || !reflect.DeepEqual(certBytes[0], derBytes1) || !reflect.DeepEqual(certBytes[1], derBytes2) {
		t.Errorf("Failed to parse derBytes with two certificates!")
	}
}

func TestParseDerBytesWithSingleCert(t *testing.T) {
	certBytes := parseDerBytes(derBytes1)

	if len(certBytes) != 1 || !reflect.DeepEqual(certBytes[0], derBytes1) {
		t.Errorf("Failed to parse derBytes with a single certificate!")
	}
}
